# coding:utf-8
# @Time    : 2021/12/10 8:54
# @Author  : Martin

"""
线下单品赠品
"""
from page_object.base import PageItems
from util.read_file import get_approve_data


class OrderGiftPage(PageItems):
    def __new__(cls, *args, **kwargs):
        instance = super().__new__(cls)
        instance._base_url = "/nspas-web-in/activityCreateFilter.do?activityType" \
                             f"=ORDERACTIVITY&promotionActType=ORDERPRESENT&comType={args[2]}"
        return instance

    def __init__(self, driver, logger, com_type):

        super().__init__(driver, logger)

    def input_act_rule(self, params):
        """
        输入赠品
        :param params:
        :return:
        """
        for param in params:
            index1 = params.index(param) + 1
            # 输入门槛金额
            self.input('css selector',
                       f'#detailinfoOrderPresentRule>table:nth-child({index1}) input[name="privilegeRule.privilegeSill"]'
                       , param['limit'])
            # 点击添加赠品
            self.click('css selector',
                       f'#detailinfoOrderPresentRule>table:nth-child({index1}) a[onclick="tradeScopeSet(this,\'new\')"]')
            # 输入可选型号数量
            self.input('id', f'promotionGoods{index1}', param['option'])

            for item in param['gifts']:
                index2 = param['gifts'].index(item) + 1
                # 点击增加
                self.click('xpath', f'//a[@onclick="presentScopeAdd(\'{index1}\')"]')

                # 输入商品编码
                self.input('css selector',
                           f'#detailinfoPresentScope{index1}>#detailedtablePresentScope:nth-child({index2}) '
                           f'input[name="activityPresentScope.commodityCode"]', item['code'])
                # 输入供应商编码
                self.input('css selector',
                           f'#detailinfoPresentScope{index1}>#detailedtablePresentScope:nth-child({index2}) '
                           f'input[name="activityPresentScope.supplierCode"]', item['supplier'])

                # 输入地点编码
                self.input('css selector',
                           f'#detailinfoPresentScope{index1}>#detailedtablePresentScope:nth-child({index2}) '
                           f'input[name="activityPresentScope.plantCode"]', item['plant'])

                # 输入单次赠送数量
                # self.input('css selector',
                #            f'#detailinfoPresentScope{index1}>#detailedtablePresentScope:nth-child({index2}) '
                #            f'input[name="activityPresentScope.singleAmount"]', item['count'])

                # 输入赠送总数量
                self.input('css selector',
                           f'#detailinfoPresentScope{index1}>#detailedtablePresentScope:nth-child({index2}) '
                           f'input[name="activityPresentScope.totalAmount"]', item['total'])

                # 输入销售价
                self.input('css selector',
                           f'#detailinfoPresentScope{index1}>#detailedtablePresentScope:nth-child({index2}) '
                           f'input[name="activityPresentScope.salePrice"]', item['sale_price'])

                # 输入宣传价
                self.input('css selector',
                           f'#detailinfoPresentScope{index1}>#detailedtablePresentScope:nth-child({index2}) '
                           f'input[name="activityPresentScope.publicityPrice"]', item['pub_price'])

            # 点击确定
            self.click('xpath', f'//a[@onclick="tradeScopeOk(\'{index1}\')"]')
            if index2 != len(params):
                # 点击增加层级
                self.click('xpath', '//a[@onclick="orderPresentAdd()"]')

    @staticmethod
    def get_approve_head_create():
        """
        获取总部新建审批流账号数据
        :return:
        """
        return get_approve_data('offline_order_gift', 'head', 'create')

    def add_single(self, params):
        """
        添加单品包含
        :param params:商品范围列表
        :return:
        """
        self._select_relation('包含')
        for param in params:
            self._input_commdty_scope(**param)
            self.click('id', 'cmmdCodeAdd')
        self.click('link text', '添加到商品范围')

    def go_to_approve(self):
        """
        首次点击提交去审批
        :return:
        """
        self.click('id', 'evaluateSubmitBtn')
